$.fn.dddata=function(){
	var self=$(this);//调用对象
	self.append("<div class='datacalendarbox' id='calendarbox'></div>");//主盒子
	var selfbox=$('#calendarbox');//添加内结构
	selfbox.append("<div class='datacalendardaytitle'><span class='dataleft dataml' id='nowsp'><span></span><span></span></span><span class='' id='datanows'></span><span class='dataright datamr' id='nowsn'><span></span><span class='active'></span></span></div>");
	selfbox.append("<div class='datacalendarday'><span>日</span><span>一</span><span>二</span><span>三</span><span>四</span><span>五</span><span>六</span></div>");
	selfbox.append("<div class='datacalendarday' id='calendarday'></div>");
	selftitl=$('#datanows');
	self.find('span').eq(0).click(function(){
		selfbox.toggle();
	});
	
	
//初始化时间
var newdata= new Date(),data_year,data_mouth,data_day;
function Initializationtime(){
	var defaulttime=self.find('span').eq(0).html();//默认时间
	if(defaulttime==''){
		defaulttime=timeformat(newdata);
	}else{
		timeformat(new Date(defaulttime));
	}
	self.find('span').eq(0).html(defaulttime);
	selftitl.html(data_year+'-'+data_mouth);
	
};
//格式化时间
function timeformat(el){
	data_year=el.getFullYear();//当前年
	data_mouth=el.getMonth()+1;//当前月
	data_day=el.getDate();//当前天
	if(data_mouth < 10){ //判断需不需要加零
	data_mouth = "0" + data_mouth; 
	} 
	if(data_day < 10){  //判断需不需要加零
		data_day = "0" + data_day; 
	} 
	return (data_year+"-"+data_mouth + "-" + data_day);
}
//判断当前月份多少天
var dangmouth;//存取天数
function howmanydays(){
	if(data_mouth==1){
		dangmouth=31;
	}else if(data_mouth==2){
		if((data_year%4==0 && data_year%100!=0) || data_year%400==0){
			dangmouth=29;
		}else{
			dangmouth=28;
		}
	}else if(data_mouth==3){
		dangmouth=31;
	}else if(data_mouth==4){
		dangmouth=30;
	}else if(data_mouth==5){
		dangmouth=31;
	}else if(data_mouth==6){
		dangmouth=30;
	}else if(data_mouth==7){
		dangmouth=31;
	}else if(data_mouth==8){
		dangmouth=31;
	}else if(data_mouth==9){
		dangmouth=30;
	}else if(data_mouth==10){
		dangmouth=31;
	}else if(data_mouth==11){
		dangmouth=30;
	}else if(data_mouth==12){
		dangmouth=31;
	}
}
//循环添加结构
var dangq=1;//每月1号
function addingstructure(){
	howmanydays(data_year,data_mouth);
	$('#calendarday').find('span').remove();
	var dydy=data_year+"-"+data_mouth+"-"+"01";
	var ce=new Date(dydy).getDay();//判断这个月一号是周几
	for(var j=0;j<ce;j++){
		$('#calendarday').append("<span></span>");
	}
	for(var i=0;i<dangmouth;i++){
		if(i<9){
				$('#calendarday').append("<span>0"+dangq+"</span>")
		}else{
				$('#calendarday').append("<span>"+dangq+"</span>")
		}
		dangq++;
	}
}
//循环添加事件
var dqyue;
function addevent(){
	var dydy=data_year+"-"+data_mouth+"-"+"01";
	var dyxx=data_year+"-"+data_mouth+"-"+data_day;
	var cx=new Date(dydy).getDay();
//	console.log(cx);
	var ce=new Date(dydy).getDay();//判断这个月一号是周几
	if(dqyue==data_mouth){
		if(cx>0&&cx<6){
			var start=data_day-cx;
//			console.log(start);
			for(var i=0;i<5;i++){
				$('#calendarday').find('span').eq(start+i+ce).css('color','#333333');
//				console.log(start+1+i);
				};
		}
		
	};
//	console.log(dqyue+'/'+data_mouth);
}
//上翻
function upturning(){
	$('#nowsp').click(function(){
		data_mouth=parseInt(data_mouth)-1;
		if(data_mouth<10){
			data_mouth='0'+data_mouth;
		};
		if(data_mouth<1){
			data_mouth=12;
			data_year=data_year-1;
		}
		$('#datanows').html(data_year+'-'+data_mouth);
		dangq=1;
		addingstructure();
		addevent();
		$('#nowsn').css('color','#333');
        $('#nowsn').find('span').eq(1).removeClass('active');
        Callback(data_year,data_mouth,data_day);
	})
}
//下翻
function turnover(){
	var noclick=data_year+'-'+data_mouth;
    if((data_mouth-1)<1){
        noclick=(data_year-1)+'-12';
    }else{
        noclick=data_year+'-'+(data_mouth-1);
    };
	$('#nowsn').click(function(){
		var dqclick=$('#datanows').html();
        console.log('='+dqclick+'/'+noclick);
		if(dqclick==noclick){

            data_mouth=parseInt(data_mouth)+1;
            if(data_mouth<10){
                data_mouth='0'+data_mouth;
            };
            if(data_mouth>12){
                data_mouth="01";
                data_year=data_year+1;
            }
            $('#datanows').html(data_year+'-'+data_mouth);
//            console.log(data_mouth);
            dangq=1;
            addingstructure();
            addevent();
			$('#nowsn').css('color','#a9a9a9');
            $('#nowsn').find('span').eq(1).addClass('active');
            Callback(data_year,data_mouth,data_day);
		}else if(dqclick<noclick){
            console.log('<');
            data_mouth=parseInt(data_mouth)+1;
            if(data_mouth<10){
                data_mouth='0'+data_mouth;
            };
            if(data_mouth>12){
                data_mouth="01";
                data_year=data_year+1;
//                console.log(data_mouth);
            }
            $('#datanows').html(data_year+'-'+data_mouth);
//            console.log(data_mouth);
            dangq=1;
            addingstructure();
            addevent();
        }else if(dqclick>noclick){
            console.log('>');
			$('#nowsn').css('color','#a9a9a9');
            $('#nowsn').find('span').eq(1).addClass('active');
            Callback(data_year,data_mouth,data_day);
		}
	})
}
//主启动
function init(){
	Initializationtime();
	addingstructure();
	upturning();
	turnover();
	dqyue=data_mouth;
	addevent();
	Callback(data_year,data_mouth,data_day)
};
init();
}
